package com.gugee.tiktok.data.consumer.blogger.core.main;

import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.gugee.tiktok.common.model.AwemeScore;
import com.gugee.tiktok.common.model.mysql.Aweme;
import com.gugee.tiktok.common.model.dal.blogger.AwemeRepository;
import com.gugee.tiktok.data.consumer.blogger.config.DataConsumerConfig;
import com.xiguaji.boot.toolkit.DateTimeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: luolifeng
 * Date: 2020-10-14 18:19
 */
@Slf4j
@Component
public class SetAwemeScoreMain {
    @Autowired
    AwemeRepository awemeRepository;
    @Autowired
    DataConsumerConfig dataConsumerConfig;
    public void handle(AwemeScore tikTokScore) {
        List<Aweme> awemeList = awemeRepository.findAwemesByoUidAndoCreateTimeGreaterThan(
                tikTokScore.getBloggerMain().getOUid(),
                DateTimeUtils.second(LocalDateTime.now().minusDays(dataConsumerConfig.getLatestDays())));

        awemeList.forEach(a ->{
            a.setScore(tikTokScore.getAwemeScore(a));
            a.setUpdateBy("GetAwemeScoreEvent");
        });

        if(awemeList.size() > 0){
            long start = System.currentTimeMillis();
            awemeRepository.batchUpdate(awemeList);
            if((System.currentTimeMillis()-start)/1000 >dataConsumerConfig.getDebugTimeLimit())
                log.info("SetAwemeScoreMain batchUpdate count:"+awemeList.size()+" use time:"+(System.currentTimeMillis()-start)/1000+"s");

            tikTokScore.getBloggerMain().setLastCollectTime(new Date());
        }

    }
}
